import uiPanel from "./../../lib/UiPanel";


const { ccclass, property } = cc._decorator;

@ccclass
export default class TableRow extends uiPanel {
	@property(cc.Prefab)
	rowItemPrefab: cc.Prefab = null;

	@property(cc.SpriteAtlas)
	atlas: cc.SpriteAtlas = null;

	@property(cc.Sprite)
	bg: cc.Sprite = null;

	itemSrc = [];
	index = 0;

	onLoad() {
		this.itemSrc = [];
	}
	onEnable() { }

	onDisable() { }

	onDestroy() { }

	show() {
		this.node.active = true;
	}
	hide() {
		this.node.active = false;
	}

	//type:0(标题)，1（中间内容），2（最后一条）, 3(中间调“黄色)
	fill(index: number, type: number, width: number, data: [], color) {
		this.show();

		this.index = index;
		var _name = ""
		if (type == 0) {
			_name = "gz1";
		} else if (type == 1) {
			_name = "gz2";
		} else if (type == 2) {
			_name = "gz3";
		} else {
			_name = "TK_xm_gz5";
		}
		this.bg.spriteFrame = this.atlas.getSpriteFrame(_name);

		this.node.width = width;
		this.bg.node.width = width;

		var _itemWidth = 0;
		if (data.length > 0) {
			_itemWidth = width / data.length;
		}

		for (var i = 0; i < data.length; i++) {
			if (i >= this.itemSrc.length) {
				var node = cc.instantiate(this.rowItemPrefab);
				node.parent = this.bg.node;
				this.itemSrc.push(node.getComponent("RowItem"));
			}
			var _type = 0;
			if (i == data.length - 1) {
				_type = 2;
			}
			this.itemSrc[i].fill(this.index, _type, _itemWidth, data[i], color);
		}
		for (var j = i; j < this.itemSrc.length; j++) {
			this.itemSrc[j].hide();
		}
	}





}
